﻿using Castle.DynamicProxy;
using System.Collections.Generic;
using System.Linq;
using System;
using System.Collections;

namespace Extensions.Interceptor.Cache.Keys;

public abstract class BaseStrategy
{
    public abstract KeyStrategyEnum KeyStrategyEnum { get;  set; }
    public abstract string GetKey(IInvocation invocation);

    /// <summary>
    /// 校验参数是否合法
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <exception cref="InvalidOperationException"></exception>
    protected bool ValidateValueType(Type type)
    {
        //因为IEnumerable、IQueryable等有延迟执行的问题，造成麻烦，因此禁止用这些类型
        // 判断是不是 泛型的类型 比如 ienumerable<string>  要把string 去掉再比较
        if (type.IsGenericType)
        {
            type = type.GetGenericTypeDefinition();
        }

        if (type == typeof(IEnumerable<>) || type == typeof(IQueryable<>) || type == typeof(IEnumerable) || type == typeof(IQueryable) || type == typeof(IAsyncEnumerable<>))
        {
            return false;
        }

        return true;
    }

}